package com.sgb.kjwl.view.ui.activity.transport;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMapCarBiddingBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.CarInfo;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.swgk.core.view.entity.DriverAuthDetailEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.MapCarBiddingEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;
import com.sgb.kjwl.view.widget.GoodsQuotedPricePopWindow3;
import com.sgb.kjwl.view.widget.MapAdressDialog;
import com.sgb.kjwl.viewmodel.transport.MapCarBiddingModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

/**
 * 发布竞价找货
 */
public class MapCarBiddingActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {

    private Thread thread;
    private static final int MSG_LOAD_DATA = 0x0001;
    private static final int MSG_LOAD_SUCCESS = 0x0002;
    private static final int MSG_LOAD_FAILED = 0x0003;
    private boolean isLoaded = false;
    private int from_type = 0;

    @Inject
    MapCarBiddingModel viewModel;

    ActivityMapCarBiddingBinding binding;

    private List<CarriageSelectEntity> carModelList;
    private int carModelPosition;
    private List<CarriageSelectEntity> carLengthList;
    private int carLengthPosition;
    private List<CarriageSelectEntity> invoiceList;
    private int invoicePosition;
    private List<CarriageSelectEntity> isDumpList;
    private int isDumpPosition;
    private List<GoodsInfoTypeEntity3> payTypeList;
    private int payTypePosition;
    // 一级联动数据
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    private List<List<AddressEntity<List<AddressEntity>>>> opt2;
    // 三级联动数据
    private List<List<List<AddressEntity>>> opt3;
    private int cityPosition1;
    private int cityPosition2;
    private int cityPosition3;
    private DriverAuthDetailEntity driverAuthDetailEntity;
    private Date today;
    private Calendar selectedDate;

    private GoodsQuotedPricePopWindow3 pricePopWindow;
    private List<CarInfo> carInfo;
    private int carPosition;
    int types=1;
    private String sendProId,sendCityId,sendAreaId,loadProId,loadCityId,loadAreaId;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder().appComponent(appComponent).transportModule(new TransportModule(this)).build().inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, MapCarBiddingActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_map_car_bidding);
        binding.setViewModel(viewModel);
        binding.backView.setOnClickListener(this);
       // binding.itemTvChengYun.setOnWholeItemClickListener(this);
        binding.itemTvCarNumber.setOnWholeItemClickListener(this);
        binding.itemTvCarModel.setOnWholeItemClickListener(this);
        binding.itemTvCarLength.setOnWholeItemClickListener(this);
        binding.itemTvLoad.setOnWholeItemClickListener(this);
        binding.itemTvIsSelfUnloading.setOnWholeItemClickListener(this);
        binding.itemTvDestination.setOnWholeItemClickListener(this);
        binding.itemTvCarLocation.setOnWholeItemClickListener(this);
        binding.itemTvTime.setOnWholeItemClickListener(this);
        binding.itemTvInvoiceRequirement.setOnWholeItemClickListener(this);
      //  binding.itemTvSettlementMethod.setOnWholeItemClickListener(this);
        binding.itemTvPricetime.setOnWholeItemClickListener(this);
        binding.itemTvCarNumber.setOnWholeItemClickListener(this);
        binding.llSendAdress.setOnClickListener(this);
        binding.ivAdress.setOnClickListener(this);
        binding.ivSend.setOnClickListener(this);
        binding.llAdress.setOnClickListener(this);
        binding.itemTvEndtime.setOnWholeItemClickListener(this);
        binding.nextView.setOnClickListener(this);
        binding.itemTvFreightIntention.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        binding.itemTvCarHeight.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        binding.itemTvLoad.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        binding.itemTvTax.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        binding.itemTvContactsNumber.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

        pricePopWindow = new GoodsQuotedPricePopWindow3(this);
        pricePopWindow.setBgView(binding.grayLayout);
        pricePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object goodsInfo, Object key2, Object key3) {
                LogUtil.v("quotedPrice","num=="+((MapCarBiddingEntity)goodsInfo).getCarNumber());
                viewModel.deTailEntity.set((MapCarBiddingEntity)goodsInfo);
                MapCarBiddingEntity entity = (MapCarBiddingEntity)goodsInfo;
                binding.itemTvChengYun.setContent(entity.getCityName() + " "+ entity.getDistrictName() + "->" + entity.getDestCityName() + " "+ entity.getDestDistrictName());
                binding.itemTvDestination.setContent(entity.getDestCityName() + " "+ entity.getDestDistrictName());
                binding.itemTvCarLocation.setContent(entity.getCityName() + " " + entity.getDistrictName());
                if(!TextUtils.isEmpty(entity.getInTime())){
                    binding.itemTvTime.setContent(DateUtils.stampToDay(entity.getInTime()));
                }
            }
        });
        binding.ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        binding.ck2.setChecked(false);
                        viewModel.deTailEntity.get().setPattern(0);
                        binding.itemTvPricetime.setVisibility(View.GONE);
                    }
            }
        });
        binding.ck2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    binding.ck1.setChecked(false);
                    viewModel.deTailEntity.get().setPattern(1);
                    binding.itemTvPricetime.setVisibility(View.VISIBLE);
                }
            }
        });

    }

    @Override
    protected void init() {
        super.init();
        opt2 = new ArrayList<>();
        opt3 = new ArrayList<>();
        selectedDate = Calendar.getInstance();
        viewModel.driverAuthDetail();
        viewModel.driverTransList();
        viewModel.getCarInfo2();
        viewModel.getUserInfo();
        viewModel.getAddressModel();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
            case R.id.next_view:
                if (TextUtils.isEmpty(binding.itemTvCarNumber.getContent())) {
                    MToast.showToast(MapCarBiddingActivity.this, "请选择车牌号");
                    return;
                } else if (TextUtils.isEmpty(binding.itemTvCarModel.getContent())) {
                    MToast.showToast(MapCarBiddingActivity.this, "请选择车型");
                    return;
                } else if (TextUtils.isEmpty(binding.itemTvLoad.getContent())) {
                    MToast.showToast(MapCarBiddingActivity.this, "请输选择车长");
                    return;
                } else if (TextUtils.isEmpty(binding.itemTvLoad.getContent())) {
                    MToast.showToast(MapCarBiddingActivity.this, "请输入载重");
                    return;
                } else if (binding.itemTvCarHeight.getContent().equals("0")) {
                    MToast.showToast(MapCarBiddingActivity.this, "高度不能为0");
                    return;
                } else if (TextUtils.isEmpty(binding.itemTvCarLocation.getContent())) {
                    MToast.showToast(MapCarBiddingActivity.this, "请选择发货地");
                    return;
                }  else if (TextUtils.isEmpty(binding.itemTvDestination.getContent())) {
                    MToast.showToast(MapCarBiddingActivity.this, "请选择目的地");
                    return;
                }else if (TextUtils.isEmpty(binding.itemTvTime.getContent())) {
                    MToast.showToast(MapCarBiddingActivity.this, "请选择装货开始时间");
                    return;
                }  else if (TextUtils.isEmpty(binding.itemTvEndtime.getContent())) {
                    MToast.showToast(this, "请选择装货结束时间");
                    return;
                }else if (TextUtils.isEmpty(binding.itemTvFreightIntention.getContent())) {
                    MToast.showToast(MapCarBiddingActivity.this, "请输入运费意向");
                    return;
                }  else if (TextUtils.isEmpty(binding.itemTvSettlementMethod.getContent())) {
                    MToast.showToast(MapCarBiddingActivity.this, "请输入接款方式");
                    return;
                } else if (binding.itemTvFreightIntention.getContent().equals("0")) {
                    MToast.showToast(MapCarBiddingActivity.this, "运费意向不能为0");
                    return;
                }else if(TextUtils.isEmpty(binding.itemTvTax.getContent())){
                    MToast.showToast(MapCarBiddingActivity.this, "请输入税率");
                    return;
                } else if (TextUtils.isEmpty(binding.itemTvContacts.getContent())) {
                    MToast.showToast(this, "请输入联系人");
                    return;
                } else  if (TextUtils.isEmpty(binding.itemTvContactsNumber.getContent())) {
                    MToast.showToast(this, "请输入手机号");
                    return;
                }
                for(CarInfo info:carInfo){
                    if(info.getCarNumber().equals(binding.itemTvCarNumber.getContent())){
                        viewModel.deTailEntity.get().setDriverNo(info.getNo());
                        break;
                    }
                }
                viewModel.deTailEntity.get().setHeightHurdle(binding.itemTvCarHeight.getContent());
                viewModel.deTailEntity.get().setFreight(binding.itemTvFreightIntention.getContent());
                viewModel.deTailEntity.get().setWight(binding.itemTvLoad.getContent());
                viewModel.deTailEntity.get().setRate(binding.itemTvTax.getContent());
                viewModel.deTailEntity.get().setContrcts(binding.itemTvContacts.getContent());
                viewModel.deTailEntity.get().setContrctsNumber(binding.itemTvContactsNumber.getContent());
                viewModel.deTailEntity.get().setPayType(binding.itemTvSettlementMethod.getContent());
                viewModel.deTailEntity.get().setDetailAddress(binding.tvSendAdress.getText().toString());
                viewModel.deTailEntity.get().setDestDetailAddress(binding.tvAdress.getText().toString());


                //车牌号
                viewModel.deTailEntity.get().setCarNumber(binding.itemTvCarNumber.getContent());
                if (driverAuthDetailEntity != null) {
                    //当前车辆位置 省
                    viewModel.deTailEntity.get().setProvinceName(driverAuthDetailEntity.getProvinceName());
                    viewModel.deTailEntity.get().setProvinceNo(driverAuthDetailEntity.getProvinceNo());
                    //当前车辆位置 市
                    viewModel.deTailEntity.get().setCityName(driverAuthDetailEntity.getCityName());
                    viewModel.deTailEntity.get().setCityNo(driverAuthDetailEntity.getCityNo());
                    //当前车辆位置 区
                    viewModel.deTailEntity.get().setDistrictName(driverAuthDetailEntity.getDistrictName());
                    viewModel.deTailEntity.get().setDistrictNo(driverAuthDetailEntity.getDistrictNo());
                }
                //备注
                viewModel.deTailEntity.get().setNote(binding.cfkEdit.getEditContent());
                viewModel.carrierPushCarrier();
                break;
            case R.id.iv_send:
            case R.id.ll_send_adress:
                types=1;
                if (!TextUtils.isEmpty(sendProId)) {
                    //跳转详细地址界面
                    viewModel.getLocationLL(sendProId,sendCityId, sendAreaId, 1);
                } else {
                    toMap(0,0);
                }
                break;
            case R.id.iv_adress:
            case R.id.ll_adress:
                types=2;
                if (!TextUtils.isEmpty(loadProId)) {
                    //跳转详细地址界面
                    viewModel.getLocationLL(loadProId,loadCityId, loadAreaId, 1);
                } else {
                    toMap(0,0);
                }
                break;
        }
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
          /*  case R.id.item_tv_cheng_yun:
                pricePopWindow.showPopwindowBottom(binding.nextView);
                break;*/
            case R.id.item_tv_car_model:
                if (carModelList != null) {
                    showCarModelPop();
                } else {
                    viewModel.carType();
                }
                break;
            case R.id.item_tv_car_length:
                if (carLengthList != null) {
                    showCarLengthPop();
                } else {
                    viewModel.carLenType();
                }
                break;
            case R.id.item_tv_is_self_unloading:
                if (isDumpList != null) {
                    showIsDumpPop();
                } else {
                    viewModel.isDump();
                }
                break;
            case R.id.item_tv_destination:
                from_type = 0;
                if (addressEntityList != null) {
                    showCityPop();
                } else {
                    viewModel.getAddressModel();
                }
                break;
            case R.id.item_tv_car_location:
                from_type = 1;
                if (addressEntityList != null) {
                    showCityPop();
                } else {
                    viewModel.getAddressModel();
                }
                break;
            case R.id.item_tv_time:
                showTimePop();
                break;
            case R.id.item_tv_invoice_requirement:
                if (invoiceList != null) {
                    showInvoicePop();
                } else {
                    viewModel.invoiceType();
                }
                break;
            case R.id.item_tv_car_number:
                if(carInfo!=null&&carInfo.size()>0) {
                    showCarInfo();
                }else{
                    viewModel.getCarInfo();

                }
                break;
            case R.id.item_tv_pricetime:
                TimePickerView timeEndPicker = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        Calendar cal1 = Calendar.getInstance();
                        cal1.setTime(date);
                        cal1.set(Calendar.HOUR_OF_DAY, 23);
                        cal1.set(Calendar.MINUTE, 59);
                        cal1.set(Calendar.SECOND, 59);

                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String format = sdf.format(date);
                        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        binding.itemTvPricetime.setContent(format);
                        viewModel.deTailEntity.get().setEndTime(DateUtils.dateTo10Timestamp3(sdf1.format(cal1.getTime()))+"");
                        //  addChildEntity.setApproachTime(format);
                    }
                }).setRangDate(Calendar.getInstance(),null).build();
                timeEndPicker.show();
                break;
            case R.id.item_tv_endtime:
                TimePickerView timeEndPicker1 = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                 /*       if (today == null)
                            today = new Date();
                        int compareTo = today.compareTo(date);
                        if (compareTo == 1) {
                            Toast.makeText(MapCarBiddingActivity.this, "不能选择小于今天的日期", Toast.LENGTH_SHORT).show();
                            return;
                        }*/
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String format = sdf.format(date);
                        binding.itemTvEndtime.setContent(format);
                        viewModel.deTailEntity.get().setShipmentEndTime(DateUtils.dateTo13Timestamp(format)+"");
                        //  addChildEntity.setApproachTime(format);
                    }
                }).setRangDate(Calendar.getInstance(),null).build();
                timeEndPicker1.show();
                break;
       /*     case R.id.item_tv_settlement_method:
                if (payTypeList != null) {
                    showPayTypePop();
                } else {
                    viewModel.getPayType();
                }
                break;*/
        }
    }
    //  返回经纬度信息
    public void getLocationLL(MapLongAndLatEntity data, int num) {

        if (!TextUtils.isEmpty(data.getLat())) {
            if (!TextUtils.isEmpty(data.getLng())) {

                toMap(Double.parseDouble(data.getLat()),Double.parseDouble(data.getLng()));

            }
        }
    }
    public void showCarInfo(List<CarInfo> carInfo){
        this.carInfo=carInfo;
        if(carInfo!=null&&carInfo.size()>0) {
            showCarInfo();
        }else{
            MToast.showToast(this,"无认证车辆，请认证");
        }
    }
    public void showCarInfo2(List<CarInfo> carInfo){
        this.carInfo=carInfo;

    }
    public void showCarInfo(){
        OptionsPickerView businessOption = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> {
            String content = carInfo.get(options1).getCarNumber();
            binding.itemTvCarNumber.setContent(content);
            carPosition = options1;
            //车型
            binding.itemTvCarModel.setContent(carInfo.get(options1).getCarTypeName());
            viewModel.deTailEntity.get().setCarTypeNo(carInfo.get(options1).getCarTypeNo());
            //载重
            binding.itemTvLoad.setContent(carInfo.get(options1).getWeight()+"");
            viewModel.deTailEntity.get().setWight(carInfo.get(options1).getWeight()+"");
            //车长
            binding.itemTvCarLength.setContent(carInfo.get(options1).getCarLenName());
            viewModel.deTailEntity.get().setCarLenNo(carInfo.get(options1).getCarLenNo());

            viewModel.deTailEntity.get().setDriverNo(carInfo.get(options1).getNo());
        }).build();
        if(carInfo!=null)
            businessOption.setPicker(carInfo);
        businessOption.setSelectOptions(carPosition);
        businessOption.show();
    }
    public void getTransInfo(List<MapCarBiddingEntity> driverAuthDetailEntity) {
        if(null==driverAuthDetailEntity || driverAuthDetailEntity.size() == 0){
            MToast.showToast(MapCarBiddingActivity.this,"暂无数据");
            return;
        }
        pricePopWindow.setDatas(driverAuthDetailEntity);
    }

    //是否自卸
    public void showIsDumpPop(List<CarriageSelectEntity> isDumpList) {
        this.isDumpList = isDumpList;
        showIsDumpPop();
    }

    private void showIsDumpPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(MapCarBiddingActivity.this, (options1, options2, options3, v) -> {
            String content = isDumpList.get(options1).getName();
            binding.itemTvIsSelfUnloading.setContent(content);
            isDumpPosition = options1;
            viewModel.deTailEntity.get().setIsDump(isDumpList.get(options1).getNo());
        }).build();
        businessOption.setPicker(isDumpList);
        businessOption.setSelectOptions(isDumpPosition);
        businessOption.show();
    }

    public void displayRole(UserMsgEntity data) {
        if(null!=data){
            binding.itemTvContactsNumber.setContent(data.getTelPhone());
            binding.itemTvContacts.setContent(data.getRealName());
        }
    }

    //我的车型
    public void showCarModelPop(List<CarriageSelectEntity> carModelList) {
        this.carModelList = carModelList;
        showCarModelPop();
    }

    private void showCarModelPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(MapCarBiddingActivity.this, (options1, options2, options3, v) -> {
            String content = carModelList.get(options1).getName();
            binding.itemTvCarModel.setContent(content);
            carModelPosition = options1;
            viewModel.deTailEntity.get().setCarTypeNo(carModelList.get(options1).getNo());
        }).build();
        businessOption.setPicker(carModelList);
        businessOption.setSelectOptions(carModelPosition);
        businessOption.show();
    }

    //我的车长
    public void showCarLengthPop(List<CarriageSelectEntity> carLengthList) {
        this.carLengthList = carLengthList;
        showCarLengthPop();
    }

    private void showCarLengthPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(MapCarBiddingActivity.this, (options1, options2, options3, v) -> {
            String content = carLengthList.get(options1).getName();
            binding.itemTvCarLength.setContent(content);
            carLengthPosition = options1;
            viewModel.deTailEntity.get().setCarLenNo(carLengthList.get(options1).getNo());
        }).build();
        businessOption.setPicker(carLengthList);
        businessOption.setSelectOptions(carLengthPosition);
        businessOption.show();
    }

    //发票要求
    public void showInvoicePop(List<CarriageSelectEntity> invoiceList) {
        this.invoiceList = invoiceList;
        showInvoicePop();
    }

    private void showInvoicePop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(MapCarBiddingActivity.this, (options1, options2, options3, v) -> {
            String content = invoiceList.get(options1).getName();
            binding.itemTvInvoiceRequirement.setContent(content);
            invoicePosition = options1;
            viewModel.deTailEntity.get().setInvoice(invoiceList.get(options1).getNo());
        }).build();
        businessOption.setPicker(invoiceList);
        businessOption.setSelectOptions(invoicePosition);
        businessOption.show();
    }

    //付款方式要求
    public void showPayType(List<GoodsInfoTypeEntity3> invoiceList) {
        this.payTypeList = invoiceList;
        showPayTypePop();
    }

    private void showPayTypePop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(MapCarBiddingActivity.this, (options1, options2, options3, v) -> {
            String content = payTypeList.get(options1).getName();
            binding.itemTvSettlementMethod.setContent(content);
            payTypePosition = options1;
            viewModel.deTailEntity.get().setPayType(payTypeList.get(options1).getNo());
        }).build();
        businessOption.setPicker(payTypeList);
        businessOption.setSelectOptions(payTypePosition);
        businessOption.show();
    }

    //计划目的地
    public void showAddressPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        this.addressEntityList = datas;
        initJsonData();
       /* if (isLoaded) {
            showCityPop();
        } else {
            mHandler.sendEmptyMessage(MSG_LOAD_DATA);
        }*/
    }
    public void setAddress(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        this.addressEntityList = datas;
    }
    public void showCityPop() {
        //条件选择器
        CityPickView cityOption = new CityPickBuilder(this, (options1, options2, options3, v) -> {
            String areaName;
            if (opt3.get(options1).get(options2).get(options3) == null) {
                areaName = "";
            } else {
                areaName = opt3.get(options1).get(options2).get(options3).getName();
                cityPosition3 = options3;
                //区
                if(from_type == 0){
                    viewModel.deTailEntity.get().setDestDistrictName(areaName);
                    viewModel.deTailEntity.get().setDestDistrictNo(opt3.get(options1).get(options2).get(options3).getNo());
                }else if(from_type == 1){
                    viewModel.deTailEntity.get().setDistrictName(areaName);
                    viewModel.deTailEntity.get().setDistrictNo(opt3.get(options1).get(options2).get(options3).getNo());
                }
            }
            String content = addressEntityList.get(options1).getName()
                    + opt2.get(options1).get(options2).getName()
                    + areaName;

            if(from_type == 0){
                binding.itemTvDestination.setContent(content);
            }else if(from_type == 1){
                binding.itemTvCarLocation.setContent(content);
            }

            cityPosition1 = options1;
            cityPosition2 = options2;
            if(from_type == 0){
                //省
                viewModel.deTailEntity.get().setDestProvinceName(addressEntityList.get(options1).getName());
                viewModel.deTailEntity.get().setDestProvinceNo(addressEntityList.get(options1).getNo());
                //市
                viewModel.deTailEntity.get().setDestCityName(opt2.get(options1).get(options2).getName());
                viewModel.deTailEntity.get().setDestCityNo(opt2.get(options1).get(options2).getNo());
            }else if(from_type == 1){
                //省
                viewModel.deTailEntity.get().setProvinceName(addressEntityList.get(options1).getName());
                viewModel.deTailEntity.get().setProvinceNo(addressEntityList.get(options1).getNo());
                //市
                viewModel.deTailEntity.get().setCityName(opt2.get(options1).get(options2).getName());
                viewModel.deTailEntity.get().setCityNo(opt2.get(options1).get(options2).getNo());
            }
            if(from_type==0){
                loadProId=addressEntityList.get(options1).getId();
                loadCityId=opt2.get(options1).get(options2).getNo();
                loadAreaId=opt3.get(options1).get(options2).get(options3).getNo();
            }else{
                sendProId=addressEntityList.get(options1).getId();
                sendCityId=opt2.get(options1).get(options2).getNo();
                sendAreaId=opt3.get(options1).get(options2).get(options3).getNo();

            }

        }).build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1, cityPosition2, cityPosition3);
        cityOption.show();
    }

    //时间选择器
    public void showTimePop() {
        TimePickerView timePicker = new TimePickerBuilder(MapCarBiddingActivity.this, (date, v) -> {
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
         /*   if (today == null)
                today = new Date();
            int compareTo = today.compareTo(date);
            if (compareTo == 1) {
                Toast.makeText(MapCarBiddingActivity.this, "请选择小于今天的日期", Toast.LENGTH_SHORT).show();
            } else {*/
                try {
                    binding.itemTvTime.setContent(format);
                    selectedDate.setTime(date);
                    //时间戳
                    long times = DateUtils.dateTo13Timestamp(format);
                    MLog.d("time", times + "");
                    viewModel.deTailEntity.get().setInTime(times + "");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            /*}*/
        }).setRangDate(Calendar.getInstance(),null).build();
        timePicker.show();
        timePicker.setDate(selectedDate);
    }

    private void initJsonData() {
        //城市列表
        List<AddressEntity<List<AddressEntity>>> cityList;
        //区域列表
        List<List<AddressEntity>> areaList;
        for (int x = 0; x < addressEntityList.size(); x++) {
            cityList = new ArrayList<>();
            areaList = new ArrayList<>();
            if (viewModel.deTailEntity.get() != null && viewModel.deTailEntity.get().getDestProvinceName() != null && viewModel.deTailEntity.get().getDestProvinceName().equals(addressEntityList.get(x).getName())) {
                cityPosition1 = x;
            }
            opt2.add(addressEntityList.get(x).getChildren());
            for (int i = 0; i < addressEntityList.get(x).getChildren().size(); i++) {
                cityList.add(addressEntityList.get(x).getChildren().get(i));
                List<AddressEntity> cityAreaList = new ArrayList<>();
                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (addressEntityList.get(x).getChildren().get(i).getChildren() == null
                        || addressEntityList.get(x).getChildren().get(i).getChildren().size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(addressEntityList.get(x).getChildren().get(i).getChildren());
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
        if (viewModel.deTailEntity.get() != null) {
            if (viewModel.deTailEntity.get().getDestCityName() != null)
                for (int x2 = 0; x2 < opt2.size(); x2++) {
                    if (viewModel.deTailEntity.get().getDestCityName().equals(addressEntityList.get(cityPosition1).getChildren().get(x2).getName())) {
                        cityPosition2 = x2;
                        break;
                    }
                }
            if (viewModel.deTailEntity.get().getDestDistrictName() != null)
                for (int x3 = 0; x3 < opt3.size(); x3++) {
                    if (viewModel.deTailEntity.get().getDestDistrictName().equals(addressEntityList.get(cityPosition1).getChildren().get(cityPosition2).getChildren().get(x3).getName())) {
                        cityPosition3 = x3;
                        break;
                    }
                }
        }
       // mHandler.sendEmptyMessage(MSG_LOAD_SUCCESS);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_LOAD_DATA:
                    if (thread == null) {//如果已创建就不再重新创建子线程了
                        thread = new Thread(() -> {
                            // 子线程中解析省市区数据
                            initJsonData();
                        });
                        thread.start();
                    }
                    break;
                case MSG_LOAD_SUCCESS:
                    showCityPop();
                    isLoaded = true;
                    thread = null;
                    break;
                case MSG_LOAD_FAILED:
                    break;
            }
        }
    };
    public void getDriverInfo(DriverAuthDetailEntity driverAuthDetailEntity) {

        //车牌号
        binding.itemTvCarNumber.setContent(driverAuthDetailEntity.getCarNumber());
        viewModel.deTailEntity.get().setCarNumber(driverAuthDetailEntity.getCarNumber());
        //车型
        binding.itemTvCarModel.setContent(driverAuthDetailEntity.getCarTypeName());
        viewModel.deTailEntity.get().setCarTypeNo(driverAuthDetailEntity.getCarTypeNo());
        //载重
        binding.itemTvLoad.setContent(driverAuthDetailEntity.getWeight() + "");
        //车长
        binding.itemTvCarLength.setContent(driverAuthDetailEntity.getCarLenName() + "");
        viewModel.deTailEntity.get().setCarLenNo(driverAuthDetailEntity.getCarLenNo());
        //联系人
        binding.itemTvContacts.setContent(driverAuthDetailEntity.getName());

    }
    public void toMap(double la,double lo){
        MapAdressDialog   dialog= new MapAdressDialog(getActivity(),null);
        dialog.setLocation(la,lo);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogs) {
                dialog.destoryMap();
            }
        });
        dialog.setDialogClickCallBack(new MapAdressDialog.DialogClickCallBack() {
            @Override
            public void onClickMarker(String adress, String pro, String city, String area, double latitude, double longtitude) {
                if(types==1) {
                    binding.tvSendAdress.setText(adress);
                    viewModel.deTailEntity.get().setDetailAddress(adress);
                    viewModel.deTailEntity.get().setLat(latitude);
                    viewModel.deTailEntity.get().setLng(longtitude);
                    viewModel.deTailEntity.get().setProvinceName(pro);
                    viewModel.deTailEntity.get().setCityName(city);
                    viewModel.deTailEntity.get().setDistrictName(area);
                    binding.itemTvCarLocation.setContent(pro + city + area);
                }else{
                    binding.tvAdress.setText(adress);
                    binding.itemTvDestination.setContent(pro + city + area);
                    viewModel.deTailEntity.get().setDestLat(latitude);
                    viewModel.deTailEntity.get().setDestLng(longtitude);
                    viewModel.deTailEntity.get().setDestDetailAddress(adress);
                    viewModel.deTailEntity.get().setDestProvinceName(pro);
                    viewModel.deTailEntity.get().setDestCityName(city);
                    viewModel.deTailEntity.get().setDestDistrictName(area);
                }
                for(AddressEntity entity:addressEntityList){
                    if(entity.getName().equals(pro)){
                        MLog.d("locationOffOder","proON "+entity.getNo()+"  "+entity.getName());
                        if(types==1) {
                            viewModel.deTailEntity.get().setProvinceNo(entity.getNo());
                        }else{
                            viewModel.deTailEntity.get().setDestProvinceNo(entity.getNo());
                        }
                        break;
                    }

                }
                boolean isbreak1=false;

                for (int i=0;i<opt2.size();i++){
                    for(int j=0;j<opt2.get(i).size();j++) {
                        if (opt2.get(i).get(j).getName().equals(city)) {
                            MLog.d("locationOffOder", "cityON1 " + opt2.get(i).get(j).getNo() + "  ");
                            if(types==1) {
                                viewModel.deTailEntity.get().setCityNo(opt2.get(i).get(j).getNo());
                            }else{
                                viewModel.deTailEntity.get().setDestCityNo(opt2.get(i).get(j).getNo());
                            }
                            isbreak1=true;
                            break;
                        }
                    }
                    if(isbreak1){
                        break;
                    }

                }
                boolean isbreak2=false;
                for (int i=0;i<opt3.size();i++){
                    for(int j=0;j<opt3.get(i).size();j++) {
                        for(int n=0;n<opt3.get(i).get(j).size();n++){
                            if(opt3.get(i).get(j).get(n)!=null) {
                                MLog.d("locationOffOder", "Direction " + opt3.get(i).get(j).get(n).getName() + "  ");
                                if (opt3.get(i).get(j).get(n).getName().equals(area)) {
                                    MLog.d("locationOffOder", "cityON2 " + opt3.get(i).get(j).get(n).getNo() + "  ");
                                    if(types==1) {
                                        viewModel.deTailEntity.get().setDistrictNo(opt3.get(i).get(j).get(n).getNo());
                                    }else{
                                        viewModel.deTailEntity.get().setDestDistrictNo(opt3.get(i).get(j).get(n).getNo());
                                    }
                                    isbreak2=true;
                                    break;
                                }
                            }
                        }
                        if(isbreak2){
                            break;
                        }
                    }
                    if(isbreak2){
                        break;
                    }
                }
            }
        });
        dialog.show();
    }
}
